#!/usr/bin/env python
# -*- encoding: utf-8 -*-

import pandas as pd

from xpy3lib.XRetryableQuery import XRetryableQuery
from xpy3lib.XRetryableSave import XRetryableSave
from sicost.AbstractDPJob import AbstractDPJob



class UNINFOJob(AbstractDPJob):





    def __init__(self,
                 p_config=None,
                 p_db_conn_mpp=None,
                 p_db_conn_rds=None,
                 p_db_conn_dbprod7=None,
                 p_account_period_start=None,
                 p_account_period_end=None):
        """

        :param p_config:
        :param p_db_conn_mpp:
        :param p_db_conn_rds:
        :param p_db_conn_dbprod7:
        :param p_unit:
        :param p_account:
        :param p_cost_center_org:
        :param p_cost_center:
        :param p_account_period_start:
        :param p_account_period_end:
        :param p_data_type: data type是 0 D M。分别是 实时、每日、每月
        :param p_wce_org: 成本科目编号
        :param p_wce:
        :param p_cal_type:
        :param p_coef:
        :param p_mon:
        """
        super(UNINFOJob, self).__init__(p_config=p_config,
                                          p_db_conn_mpp=p_db_conn_mpp,
                                          p_db_conn_rds=p_db_conn_rds,
                                          p_db_conn_dbprod7=p_db_conn_dbprod7,
                                          p_account_period_start=p_account_period_start,
                                          p_account_period_end=p_account_period_end)



    def do_execute(self):
        """
        """
        self.logger.info('UNINFOJob.do_execute')

        self.__step_0()
        self.__step_1()


        super(UNINFOJob, self).do_execute()

    def __step_0(self):
        sql = " DELETE FROM " \
              " BGTARAS1.T_ADS_FACT_SICB_UNINFO" \
              " WHERE 1=1 " \
              " AND REC_CREATE_TIME >= '%s'" \
              " AND REC_CREATE_TIME < '%s'" % (self.account_period_start,self.account_period_end)



        print(sql)
        self.db_conn_rds.execute(sql)

    def __step_1(self):
        sql = " SELECT " \
              " A.REC_CREATOR," \
              " A.REC_CREATE_TIME," \
              " A.REC_CREATOR as REC_REVISOR," \
              " A.REC_CREATE_TIME as REC_REVISE_TIME," \
              " A.UNIT_CODE," \
              " A.OUT_MAT_NO, " \
              " A.SHIFT_NO, " \
              " A.SHIFT_GROUP, " \
              " A.PROD_START_TIME, " \
              " A.PROD_END_TIME, " \
              " A.PROD_DATE, " \
              " A.IN_MAT_NO_1, " \
              " B.IN_MAT_THICK, " \
              " B.IN_MAT_WIDTH*10 as IN_MAT_WIDTH," \
              " B.IN_MAT_WT, " \
              " B.MAT_ACT_THICK," \
              " B.MAT_ACT_WIDTH, " \
              " A.OUT_MAT_ACT_LEN as MAT_ACT_LEN," \
              " B.MAT_ACT_WT," \
              " B.SG_SIGN," \
              " B.ST_NO," \
              " B.ANNEAL_DIAGRAM_CODE," \
              " B.COAT_CODE," \
              " A.TOP_COAT_WT_MGO," \
              " A.BOT_COAT_WT_MGO," \
              " B.TOP_COAT_THICK_AVG," \
              " B.BOT_COAT_THICK_AVG," \
              " A.SCRAP_WT_HEAD_OUT," \
              " A.SCRAP_WT_TAIL_OUT," \
              " A.TRIM_SCRAP_WIDTH_DS," \
              " A.TRIM_SCRAP_WIDTH_WS," \
              " A.DUMMY_COIL_FLAG" \
              " FROM MMSIJ4.TMMSIJ402 A" \
              " LEFT JOIN  TMSIJ4.TTMSIJ492 B" \
              " ON A.UNIT_CODE=B.UNIT_CODE AND A.OUT_MAT_NO=B.COIL_NO AND LEFT(A.PROD_END_TIME,10)=LEFT(B.END_PROD_TIME,10)  " \
              " WHERE" \
              " A.UNIT_CODE IN ('Q112','Q212','Q312','Q114','Q214','Q418','Q102','Q202','Q302','Q103','Q203','Q303')" \
              " AND A.REC_CREATE_TIME >=  '%s'  " \
              " AND A.REC_CREATE_TIME< '%s'" % (self.account_period_start,self.account_period_end)

        print(sql)

        df = XRetryableQuery(p_db_conn=self.db_conn_dbprod7, p_sql=sql, p_max_times=5).redo()
        success = df.empty is False
        if success is False:
            return
        df1 = df
        df1.columns = df1.columns.str.upper()

        df1.rename(columns={'REC_CREATOR': 'REC_CREATOR'}, inplace=True)
        df1.rename(columns={'REC_CREATE_TIME': 'REC_CREATE_TIME'}, inplace=True)
        df1.rename(columns={'REC_REVISOR': 'REC_REVISOR'}, inplace=True)
        df1.rename(columns={'REC_REVISE_TIME': 'REC_REVISE_TIME'}, inplace=True)
        df1.rename(columns={'UNIT_CODE': 'UNIT_CODE'}, inplace=True)
        df1.rename(columns={'OUT_MAT_NO': 'PROD_COILNO'}, inplace=True)
        df1.rename(columns={'SHIFT_NO': 'SHIFT'}, inplace=True)
        df1.rename(columns={'SHIFT_GROUP': 'CLASS'}, inplace=True)
        df1.rename(columns={'PROD_START_TIME': 'PRODUCE_START_TIME'}, inplace=True)
        df1.rename(columns={'PROD_END_TIME': 'PRODUCE_END_TIME'}, inplace=True)
        df1.rename(columns={'PROD_DATE': 'PRODUCE_DATE'}, inplace=True)
        df1.rename(columns={'IN_MAT_NO_1': 'ENTRY_COILNO'}, inplace=True)
        df1.rename(columns={'IN_MAT_THICK': 'ENTRY_MAT_THICK'}, inplace=True)
        df1.rename(columns={'IN_MAT_WIDTH': 'ENTRY_MAT_WIDTH'}, inplace=True)
        df1.rename(columns={'IN_MAT_WT': 'ENTRY_MAT_WT'}, inplace=True)
        df1.rename(columns={'MAT_ACT_THICK': 'MAT_ACT_THICK'}, inplace=True)
        df1.rename(columns={'MAT_ACT_WIDTH': 'MAT_ACT_WIDTH'}, inplace=True)
        df1.rename(columns={'MAT_ACT_LEN': 'MAT_ACT_LEN'}, inplace=True)
        df1.rename(columns={'MAT_ACT_WT': 'ACT_OUTPUT_WT'}, inplace=True)
        df1.rename(columns={'SG_SIGN': 'SG_SIGN'}, inplace=True)
        df1.rename(columns={'ST_NO': 'STEELNO'}, inplace=True)
        df1.rename(columns={'ANNEAL_DIAGRAM_CODE': 'ANNEAL_CURVE'}, inplace=True)
        df1.rename(columns={'COAT_CODE': 'COATING_TYPE'}, inplace=True)
        df1.rename(columns={'TOP_COAT_WT_MGO': 'TOP_COATING_WT'}, inplace=True)
        df1.rename(columns={'BOT_COAT_WT_MGO': 'BOT_COATING_WT'}, inplace=True)
        df1.rename(columns={'TOP_COAT_THICK_AVG': 'TOP_COATING_THICK_AVG'}, inplace=True)
        df1.rename(columns={'BOT_COAT_THICK_AVG': 'BOT_COATING_THICK_AVG'}, inplace=True)
        df1.rename(columns={'SCRAP_WT_HEAD_OUT': 'EXIT_HEADCUT_SCRAP_WT'}, inplace=True)
        df1.rename(columns={'SCRAP_WT_TAIL_OUT': 'EXIT_TAILCUT_SCRAP_WT'}, inplace=True)
        df1.rename(columns={'TRIM_SCRAP_WIDTH_DS': 'DSIDE_TRIMMING_SCRAP_WID'}, inplace=True)
        df1.rename(columns={'TRIM_SCRAP_WIDTH_WS': 'OSIDE_TRIMMING_SCRAP_WID'}, inplace=True)
        df1.rename(columns={'DUMMY_COIL_FLAG': 'DMY_FLAG'}, inplace=True)
        df1.rename(columns={'PRE_UNIT_CODE': 'SOURCE_UNIT'}, inplace=True)
        df1.rename(columns={'IN_MAT_LEN': 'ENTRY_MAT_LEN'}, inplace=True)

        XRetryableSave(p_db_conn=self.db_conn_rds, p_table_name='T_ADS_FACT_SICB_UNINFO', p_schema='BGTARAS1',
                       p_dataframe=df1,
                       p_max_times=5).redo()



        return
